﻿#include "agreement.h"
#include <math.h>
#pragma execution_character_set("utf-8");

void GetStateData(FrameFormat* format, State *state)//状态量
{
    QStringList datastr;
    QList<int> dataList;
    for(int i=0; i<2; i++){
        QString binary = QString("%1").arg(format->DATA[i]&0xFF,8,2,QLatin1Char('0'));
        //for(int i=binary.size()-1; i>=0; i--){ //反序
        for(int i=0; i<binary.size(); i++){
            QString str = binary.at(i);
            datastr.append(str);
            dataList.append(str.toInt());
        }
    }
    state->datetime = QDateTime::currentDateTime();
    state->data = datastr.join(",");
    state->dataList = dataList;

    return ;
}

void JudgeData(Simulation *sim, FrameFormat* format, Equipment  *equ,State *state,int a)
{
    //FrameFormat* format=form;
    if(format->HEAD!=0xEEFF)
    {
        return ;
        qDebug()<<"数据获取错误！";
    }
    quint8 cmd=format->CMD;
    if(cmd==CMD1)//空闲查询
    {
        format->CRC=format->DATA[1];
        for(int i=0;i<16;i++)
        {
            format->DATA[i]=0;
        }
    }
#if  0
    else if(cmd==CMD2)//状态量查询
    {
        format->CRC=format->DATA[6];
        for(int i=6;i<20;i++)
        {
            format->DATA[i]=0;
        }
        GetStateData(format,state);
    }
    else if(cmd==CMD3)//模拟量查询
    {  format->CRC=format->DATA[14];
        for(int i=14;i<20;i++)
        {
            format->DATA[i]=0;
        }
        GetSimulationData(sim,format,equ,a);
    }
#endif
    else if(cmd==CMD2)//状态+模拟
    {
        a=2;
        GetStateData(format,state);
        GetSimulationData(sim,format,equ,a);
    }
    else{
        return;
        qDebug()<<"数据获取错误！";
    }
}

void GetSimulationData(Simulation *sim, FrameFormat *format, Equipment  *equ, int a)
{
    quint8 data[16];
    memcpy(data,format->DATA,sizeof(format->DATA));
    sim->datetime = QDateTime::currentDateTime();
    sim->devid       = format->DEVID;
    sim->launch      = PowerAriIn(data[0+a],equ);
    sim->reflect     = PowerAriRef(data[1+a],equ);
    sim->highFreq    = OtherAri(data[2+a],equ->cosgp);
    sim->FOFZ        = OtherAri(data[3+a],equ->cos_140);
    sim->FOE         = OtherAri(data[4+a],equ->cos_18);
    sim->OT          = OtherAri(data[5+a],equ->cos_12);
    sim->OE          = OtherAri(data[6+a],equ->cos_z18);
    sim->TF          = OtherAri(data[7+a],equ->cos_24);
    sim->audio       = OtherAri(data[8+a],equ->cosyp);
    sim->putting     = OtherAri(data[9+a],equ->coszf);
    sim->modulate    = OtherAri(data[10+a],equ->costz);
    sim->drive       = OtherAri(data[11+a],equ->costd);

    quint16 arr;
    if(IsLittleEndian()){//为小端机时
        QByteArray array;
        array.append(data[13+a]);
        array.append(data[12+a]);
        memcpy(&arr,array.data(),2);
    }else{//为大端机时
        memcpy(&arr,data+12+a,2);
    }
    float fre=QString::number(arr,'f',1).toFloat();
    sim->fre         = fre;
}

float PowerAriIn(quint8 powHex,Equipment  *equ)
{
    int pow=QString::number(powHex,10).toInt();

        //float as=(pow*pow/par_50)*coe;
        float a=pow*pow;
        float b =a/par_1;
        float as=b*equ->cosrs;
        QString str=QString::asprintf("%.1f",as);//as四舍五入保留一位小数
        float powIn_50=str.toFloat();
        return powIn_50;

}

float PowerAriRef(quint8 powHex, Equipment  *equ)
{
    float pow=QString::number(powHex,10).toFloat();
    float a=pow*pow;
    float b =a/par_1;
    float as=b*equ->cosfs;
    QString str=QString::asprintf("%.1f",as);
    float powRef=str.toFloat();
    return powRef;
}

float OtherAri(quint8 powHex, float coe)
{
    float pow=QString::number(powHex,10).toFloat();
    float a=pow/par_2;
    float b=a*coe;
    QString str=QString::asprintf("%.1f",b);
    float other=str.toFloat();
    return other;
}

bool IsLittleEndian()//判断大小端机
{
    union{
        short a;//2字节
        char  b;//1字节
    }test;
    test.a=0x0061;//a
    return test.b == 'a';
}




